package com.cqan.survey.rest.impl;

import java.util.Calendar;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.core.HttpHeaders;

import org.springframework.stereotype.Component;

import com.cqan.common.Constant;
import com.cqan.common.ResultResp;
import com.cqan.log.entity.GenericLog;
import com.cqan.log.service.GenericLogService;
import com.cqan.service.rest.BaseRestService;
import com.cqan.survey.entity.Survey;
import com.cqan.survey.rest.SurveyRestService;
import com.cqan.survey.service.SurveyService;
import com.cqan.util.http.HttpStatus;

@Component("surveyRestService")
public class SurveyRestServiceImpl extends BaseRestService implements SurveyRestService {

	@Resource(name="surveyService")
	private SurveyService surveyService;

	@Resource(name="genericLogService")
	private GenericLogService genericLogService;
	
	@Override
	public ResultResp feedback(String title,String content,HttpHeaders headers) {
		ResultResp resp = new ResultResp();
		try {
			Map<String, String> header = Constant.getHeader(headers);
			Survey survey = new Survey();
			survey.setContent(encodeing(content));
			survey.setTitle(encodeing(title));
			survey.setCreateTime(Calendar.getInstance().getTime());
			String uid = header.get("uid");
			String ab = header.get("ab");
			if (ab==null||!"".equals(ab)) {
				survey.setPid(Long.parseLong(ab));
			}
			if (uid==null||!"".equals(uid)) {
				survey.setUid(Long.parseLong(uid));
			}
			survey.setDeviceName(header.get("ua"));
			survey.setUpdateTime(Calendar.getInstance().getTime());
			surveyService.add(survey);
			GenericLog log = new GenericLog(headers, req.getRemoteAddr(),"feedback");
			genericLogService.add(log);
			resp.setInfocode(HttpStatus.OK);
		} catch (Exception e) {
			resp.setInfocode(HttpStatus.INTERNAL_SERVER_ERROR);
			e.printStackTrace();
		}
		return resp;
	}
}
